//
//  StatusPictureView.swift
//  sinaweibo
//
//  Created by mac on 15/11/15.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SDWebImage

private let PictureCellId = "PictureCellId"
private let pictureMargin:CGFloat = 5

class StatusPictureView: UICollectionView {

    var imageURLs:[NSURL]? {
        didSet{
//            print(imageURLs)
            //刷新数据
            reloadData()
            
            let viewSize = calceView()
            self.snp_updateConstraints{ (make) -> Void in
                make.width.equalTo(viewSize.width)
                make.height.equalTo(viewSize.height)
            }
            
            testLabel.text = "\(imageURLs?.count ?? 0)"
        }
    }
    
     private func calceView() -> CGSize {
    
        let colum:CGFloat = 3
        let maxWidth = screenW - 2 * statusCellMargin
        
        //单个图片的宽度
        let itemWidth = (maxWidth - 2 * pictureMargin) / colum
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        let imageCount = imageURLs?.count ?? 0
        
        if imageCount == 0 {
            return CGSizeZero
        }
        
        if imageCount == 1 {
            //获取单张被缓存的图片
//            let key = imageURLs?.last?.absoluteString
//            let image = SDImageCache.sharedImageCache().imageFromDiskCacheForKey(key!)
//            print(image) 
            let imageSize = CGSize(width: 180, height: 120)
            layout.itemSize = imageSize
            return imageSize
        }
        
        if imageCount == 4 {
            let w = itemWidth * 2 + pictureMargin
            return CGSize(width: w, height: w)
        }
        
        let row = CGFloat((imageCount - 1) / 3 + 1)
        let w = maxWidth
        let h = row * itemWidth + (row - 1) * pictureMargin
        return CGSize(width: w, height: h)
    }
    
    init() {
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = pictureMargin
        layout.minimumLineSpacing = pictureMargin
        
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        registerClass(PictureCell.self, forCellWithReuseIdentifier: PictureCellId)
        setupUI()
        //设置数据源
        dataSource = self
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
    
        backgroundColor = UIColor.randomColor()
        
        addSubview(testLabel)
        
        testLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
    }
    
    //懒加载testLabel
    private lazy var testLabel:UILabel = UILabel(title: "", size: 25, color: UIColor.redColor())

}

extension StatusPictureView:UICollectionViewDataSource {

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //==================
//        return (imageURLs?.count)! ?? 0
        //==================
        return imageURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PictureCellId, forIndexPath: indexPath) as! PictureCell
        
        cell.url = imageURLs![indexPath.item]
        
        return cell
    }
}

class PictureCell: UICollectionViewCell {
    
    var url: NSURL? {
        didSet{
            imageView.sd_setImageWithURL(url, placeholderImage: nil)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
    
        contentView.addSubview(imageView)
        
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
    }
    
    private lazy var imageView:UIImageView = {
    
        let iv = UIImageView()
        //设置图片样式
        iv.contentMode = UIViewContentMode.ScaleAspectFill
        //手写代码必须手动剪裁
        iv.clipsToBounds = true
        
        return iv
    }()
    
    
    
    
    
    
}








